package com.ckr.springcloud.controller;

import com.ckr.springcloud.pojo.Dept;
import com.ckr.springcloud.service.DeptService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author Shadowckr
 * @create 2022-01-20 23:15
 */

@RestController
@RequestMapping("/dept")
public class DeptController {
    @Resource
    private DeptService service;

    //一旦调用服务方法失败并抛出了错误信息后会自动调用HystrixCommand标注好的fallbackMethod调用类中指定方法
    @HystrixCommand(fallbackMethod = "processHystrix_Get")
    @GetMapping("/get/{id}")
    public Dept get(@PathVariable("id") Long id) {
        Dept dept = service.queryById(id);
        if (dept == null) {
            throw new RuntimeException("该id：" + id + "没有对应的的信息！");
        }
        return dept;
    }

    @SuppressWarnings("MVCPathVariableInspection")
    public Dept processHystrix_Get(@PathVariable("id") Long id) {
        return new Dept()
                .setDeptno(id)
                .setDname("该id：" + id + "没有对应的信息，null--@HystrixCommand")
                .setDb_source("no this database in MySQL");

    }
}
